const { find } = require('./data/org-units')
const { find: findRoom } = require('./data/rooms')

const base = '/userserver'

module.exports = [{
  url: `${base}/qrcode/shop/[1-9][0-9]*`,
  type: 'get',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const orgUnit = find(id)
    if (orgUnit) {
      const { addr, detailAddress } = orgUnit.address || {}
      return { success: true, data: { uuid: orgUnit.id, name: orgUnit.name, addr, detailAddress, expiredTime: new Date() }}
    } else {
      return { success: false, message: '店铺不存在' }
    }
  }
}, {
  url: `${base}/qrcode/room/[1-9][0-9]*`,
  type: 'get',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const room = findRoom(id)
    if (room) {
      const orgUnit = find(room.orgUnitId) || {}
      const { addr, detailAddress } = orgUnit.address || {}
      return { success: true, data: { uuid: room.id, name: room.name, addr, detailAddress, expiredTime: new Date() }}
    } else {
      return { success: false, message: '房间不存在' }
    }
  }
}]
